<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ToolsController
 *
 * @author Administrator
 */
class BackupController extends Controller {

    public function actionIndex() {
        $model = new BackupForm();
        $request = Yii::app()->request;
        if ($request->getIsPostRequest()) {
            
        }
        if ($request->getParam('dir')) {
            $model->dir = base64_decode($request->getParam('dir'));
        }
        $this->render('index', array('model' => $model));
    }
    /**
     *浏览目录或者文件 
     */
    public function actionView() {
        $model = new BackupForm();
        $request = Yii::app()->request;
        $dir = $request->getParam('dir');
        if ($dir) {
            $dirname = base64_decode($dir);
            $filename = $model->getRoot(). DIRECTORY_SEPARATOR . $dirname;
            if (is_dir($filename)) {

                $model->dir = $model->dir.DIRECTORY_SEPARATOR.$dirname;
                $this->render('export', array('model' => $model));
            } else {
                if (is_readable($filename)) {
                    Yii::app()->request->xSendFile($filename);
                } else {
                    Yii::log(Yii::t('log', $filename . '没有读的权限'), 'error', __CLASS__);
                }
            }
        }
    }
    public function actionImport(){
        $request = Yii::app()->request;
        $file = base64_decode($request->getParam("dir"));
        if( $file ){
            $backup = new BackupForm();
            $backup->import($file);
        }
    }
    /**
     *删除目录或者文件 
     */
    
    public function actionDelete() {
        $model = new BackupForm();
        $request = Yii::app()->request;
        $dir = $request->getParam('dir');
        if ($dir) {
            $dirname = base64_decode($dir);
            if ($dirname) {
                //$filename = Yii::app()->basePath.DIRECTORY_SEPARATOR.$model->dir.DIRECTORY_SEPARATOR.$dirname;
                echo $model->delete($dirname);
            }
        }
    }

    public function actionExport() {
        $model = new BackupForm('export');
        $request = Yii::app()->request;
        if ($request->getIsPostRequest()) {
            $model->attributes = $request->getPost('BackupForm');
            if ($model->validate()===true) {
                if ($model->doExport()) {
                    $this->render('/layouts/message', array('message' => Yii::t('default', '备份成功')));
                    Yii::app()->end();
                }
            }
        }
        $this->render('export', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'config-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
